home *** CD-ROM | disk | FTP | other *** search
- Path: castle.nando.net!news
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.lang.c
- Subject: Re: Converting Strings to Upper Case
- Date: 17 Mar 1996 18:20:06 GMT
- Organization: Nando.net Public Access
- Message-ID: <4ihl4m$4ca@castle.nando.net>
- References: <4ifra6$52i@scipio.cyberstore.ca> <4ih7l3$526@thrush.sover.net>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: grail2113.nando.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4ih7l3$526@thrush.sover.net>, mountain@sover.net (Steve Mount) writes:
- >In article <4ifra6$52i@scipio.cyberstore.ca>, ejw@news.cyberstore.ca says...
- >>I need to write a function to convert a string containg upper or lower case
- >>characters to the opposite case. Something like:
- >>
- >> void libConvertUpperCase(char *str); and
- >> void libConvertLowerCase(char *str);
- >>
- >>and the string would be modified. I just can't seem to wrap my head around
- >>the best way that I know is better than writing a for loop to check each
- >>element in the array?
- >
- >My home compiler has strlwr and strupr functions. At work, we don't, so
- >just to make it work, quick and dirty, I did:
- >
- >void strnlwr(char *buffer, int len)
- >{
- >register int i;
- >for (i=0;i<len;i++) buffer[i] = tolower(buffer[i]);
- >return;
- >}
-
- A couple of comments on this solution. The inclusion of the parameter
- len is problematic. First the user must obtain a length before the call.
- Second, not having a check in the function, conversion may continue
- beyond the end of the string.
-
- Also the function returns nothing. It is convenient to return a pointer.
-
- Also, char may be signed and should therefore be cast before apply-
- ing tolower(). Finally, it is always a good idea to declare a function
- before using it.
-
- #include <ctype.h>
-
- char *mystr2lwr( char *s )
- {
- char *t = s;
-
- if ( t != NULL )
- while ( *s )
- *s++ = tolower( (unsigned char)*s );
-
- return t;
- }
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-